package com.jinkeen.xiaxie.pictureInPicture

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.hardware.display.DisplayManager
import android.media.MediaRouter
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.Display
import android.widget.Button
import android.widget.VideoView
import com.jinkeen.xiaxie.R

class PictureInPictureActivity : AppCompatActivity() {
    private var videoPlayer: VideoView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_picture_in_picture)
        //启动Activity让用户授权
        if (!Settings.canDrawOverlays(this)) {
            val intent = Intent(
                Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:com.heiko.presentationtest")
            )
            startActivityForResult(intent, 1010)
            return
        }

        findViewById<Button>(R.id.btn_show).setOnClickListener {
/*            val mediaRouter = getSystemService(MEDIA_ROUTER_SERVICE) as MediaRouter
            val route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_AUDIO)
            if (route != null) {
                val presentationDisplay: Display? = route.presentationDisplay
                if (presentationDisplay != null) {
                    val myPresentation = SecondPresentation(this, presentationDisplay)
                    myPresentation.show()
                }
            }*/
            Log.i("ZZZZ", "click")
            val mDisplayManager = getSystemService(DISPLAY_SERVICE) as DisplayManager
            val displays = mDisplayManager.displays
            if (displays.size > 1) {
                val dm = DisplayMetrics()
                displays[1].getMetrics(dm)
                Log.i(
                    "ZZZZ",
                    "width:${displays[1].width} height:${displays[1].height} density:${dm.density}"
                )
                //displays[0] 主屏，displays[1] 副屏
                val myPresentation = SecondPresentation(this, displays[1])
                myPresentation.show()
            }
        }
    }
}